Reordering the columns in a data frame
Problem
You want to do reorder the columns in a data frame.
Solution
# A sample data frame data <- data.frame (id=1:3, weight=c(20,27,24), size=c("small", "large", "medium")) # id weight size # 1 20 small # 2 27 large # 3 24 medium # Reorder by column number data <- data[, c(1,3,2)] # id size weight # 1 small 20 # 2 large 27 # 3 medium 24 # Reorder by column name data <- data[, c("size", "id", "weight")] # size id weight # small 1 20 # large 2 27 # medium 3 24